package org.example.practiceexamples.service;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.practiceexamples.dox.Appointment;
import org.example.practiceexamples.dox.Course;
import org.example.practiceexamples.dox.Lab;
import org.example.practiceexamples.dox.User;
import org.example.practiceexamples.dto.LabAppointment;
import org.example.practiceexamples.dto.UserAppointment;
import org.example.practiceexamples.dto.UserCourse;
import org.example.practiceexamples.dto.UserLab;
import org.example.practiceexamples.exception.Code;
import org.example.practiceexamples.exception.XException;
import org.example.practiceexamples.repository.AppointmentRepository;
import org.example.practiceexamples.repository.CourseRepository;
import org.example.practiceexamples.repository.LabRepository;
import org.example.practiceexamples.repository.UserRepository;
import org.example.practiceexamples.tools.JsonTemp;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class AdminService {

    private final UserRepository userRepository;
    private final LabRepository labRepository;
    private final AppointmentRepository appointmentRepository;
    private final CourseRepository courseRepository;

    @Transactional
    public void saveTeacher(User teacher){
        userRepository.save(teacher);
    }

    @Transactional
    public void saveLab(Lab lab){
        labRepository.save(lab);
    }

    @Transactional
    public void saveAppointment(Appointment appointment){
        appointmentRepository.save(appointment);
    }

    @Transactional
    public void saveCourse(Course course){
        courseRepository.save(course);
    }


    @Transactional
    public List<User> findAllUsers(){
        return userRepository.findAllUsers();
    }

    @Transactional
    public List<Lab> findAllLabs(){
        return labRepository.findAllLabs();
    }

    @Transactional
    public List<Appointment> findAllAppointments(){
        return appointmentRepository.findAllAppointments();
    }

    @Transactional
    public List<Course> findAllCourses(){
        return courseRepository.findAllCourses();
    }


    @Transactional
    public User findUserById(String id){
        return userRepository.findUserByUid(id);
    }

    @Transactional
    public List<Course> findCoursesByAccount(String account){
        UserCourse userCourse= userRepository.findUserCourses(account);
        return userCourse.getCourses();
    }

    @Transactional
    public List<Course> findCoursesById(String id){
        UserCourse userCourse= userRepository.findUserCourses(id);
        return userCourse.getCourses();
    }

    @Transactional
    public Course findCourseById(String id){
        Course course= courseRepository.findCourseById(id);
        return course;
    }

    @Transactional
    public List<Lab> findLabsByAccount(String account){
        UserLab userLab = userRepository.findUserLabByAccount(account);
        return userLab.getLabs();
    }

    @Transactional
    public List<Lab> findLabsById(String uid){
        UserLab userLab = userRepository.findUserLabByUid(uid);
        return userLab.getLabs();
    }

    @Transactional
    public Lab findLabByLname(String lname){
        Lab lab = labRepository.findLabByName(lname);
        return lab;
    }

    @Transactional
    public List<Appointment> findAllAppointments(String account){
        UserAppointment userAppointment = userRepository.findUserAppointmentByAccount(account);
        return userAppointment.getAppointments();
    }

    @Transactional
    public List<Appointment> findAppointmentsByCourse(String cid){
        return courseRepository.findCourseAppointmentById(cid).getAppointments();
    }

    @Transactional
    public List<Appointment> findAppointmentsByLid(String lid){
        return labRepository.findLabAppointmentByLid(lid).getAppointments();
    }

    @Transactional
    public List<Appointment> findAppointmentsByLname(String lname){
        return labRepository.findLabAppointmentByName(lname).getAppointments();
    }

    @Transactional
    public Appointment findAppointmentById(String aid){
        return appointmentRepository.findAppointmentById(aid);
    }



    @Transactional
    public void updateStatus(String account, Integer status){
        userRepository.updateStatusByAccount(account, status);
    }

    @Transactional
    public void updateRole(String account, String role){
        userRepository.updateRoleByAccount(account, role);
    }

    @Transactional
    public void updateProfession(String account, String profession){
        userRepository.updateProfessionByAccount(account, profession);
    }

    @Transactional
    public void updatePassword(String account, String password){
        userRepository.updatePasswordByAccount(account, password);
    }

    @Transactional
    public void updateQuantity(int quantity,String name){
        labRepository.updateQuantityByName(quantity,name);
    }

    @Transactional
    public void updateUserDescription(String account, String description){
        userRepository.updateDescriptionByAccount(account, description);
    }

    @Transactional
    public void updateDescription(String description,String name){
        labRepository.updateDescriptionByName(description,name);
    }

    @Transactional
    public void updateManage(String manage,String name){
        labRepository.updateManageByName(manage,name);
    }

    @Transactional
    public void updateState(int state,String name){
        labRepository.updateStateByName(state,name);
    }

    @Transactional
    public void updateQuantity(String cid,int quantity){
        courseRepository.updateQuantity(cid,quantity);
    }


    @Transactional
    public void deleteUser(String account){
        userRepository.deleteUserByAccount(account);
    }

    @Transactional
    public void deleteLab(String name){
        labRepository.deleteLabByName(name);
    }

    @Transactional
    public void deleteCourse(String cid){
        courseRepository.deleteById(cid);
    }

    @Transactional
    public void deleteAppointment(String labName, int week, int day, int section){
        appointmentRepository.deleteAppointmentByLName(labName, week, day, section);
    }

    @Transactional
    public void deleteAppointmentById(String aid){
        appointmentRepository.deleteAppointmentById(aid);
    }

}
